home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / PasclStr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  4.2 KB  |  188 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PasclStr.cpp
  3.  
  4.     Contains:    Pascal string manipulation routines
  5.  
  6.     Owned by:    Nick Pilch
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. #ifndef _ODTYPES_
  12. #include "ODTypes.h"
  13. #endif
  14.  
  15. #ifndef _PASCLSTR_
  16. #include "PasclStr.h"
  17. #endif
  18.  
  19. #ifndef _ISOSTR_
  20. #include "ISOStr.h"
  21. #endif
  22.  
  23. #ifndef _ODMEMORY_
  24. #include "ODMemory.h"
  25. #endif
  26.  
  27. #ifndef _ITEXT_
  28. #include "IText.h"
  29. #endif
  30.  
  31. #ifndef __MEMORY__
  32. #include <Memory.h>
  33. #endif
  34.  
  35. #ifndef __STRING__
  36. #include <string.h>
  37. #endif
  38.  
  39. #ifndef __CTYPE__
  40. #include <ctype.h>
  41. #endif
  42.  
  43. #ifndef __SCRIPT__
  44. #include <Script.h>
  45. #endif
  46.  
  47. #ifdef __SC__
  48.     #ifndef __LANGUAGE__
  49.     #include <Language.h>
  50.     #endif
  51. #endif // __SC__
  52.  
  53. #pragma segment ODPascalString
  54.  
  55. //==============================================================================
  56. // Functions
  57. //==============================================================================
  58.  
  59. void CopyPascalString(Str255 destString, ConstStr255Param srcString)
  60. {
  61.     ODBlockMove((ODPtr)srcString, (ODPtr)destString, srcString[0] + 1L);
  62. }
  63.  
  64. void CopyISOStr2PStr(Str255 destString, const ODISOStr srcString)
  65. {
  66.     ODSize len = ODISOStrLength(srcString);
  67.     ODBlockMove((ODPtr)srcString, &(destString[1]), (ODULong)len);
  68.     destString[0] = (unsigned char) len;
  69. }
  70.  
  71. void ConcatPascalStrings(Str255 destString, ConstStr255Param srcString)
  72. {
  73.     ODSShort maxSrcLength = 255 - destString[0];
  74.     if ( srcString[0] > maxSrcLength )
  75.     {
  76.         Str255 tmpStr;
  77.         ODBlockMove( srcString, tmpStr, srcString[0] + 1 );
  78.         maxSrcLength = ClipStringToBytes( (StringPtr)srcString,
  79.                 maxSrcLength, smCurrentScript );
  80.     }
  81.     else
  82.         maxSrcLength = srcString[0];
  83.  
  84.     ODBlockMove((ODPtr)(srcString + 1),
  85.                     (ODPtr)(destString + destString[0] + 1), maxSrcLength);
  86.     destString[0] += maxSrcLength;
  87. }
  88.  
  89. ODBoolean EqualPascalStrings(ConstStr255Param str1, ConstStr255Param str2)
  90. {
  91.     if (str1[0] != str2[0])
  92.         return kODFalse;
  93.     for (int i = 1; i <= str1[0]; i++)
  94.     {
  95.         if (tolower(str1[i]) != tolower(str2[i]))
  96.             return kODFalse;
  97.     }
  98.     return kODTrue;
  99. }
  100.  
  101. StringPtr IntlToPStr(ODIText* intlText, StringPtr pstr)
  102. {
  103.     return GetITextString(intlText,pstr);
  104. }
  105.  
  106. ODIText* PStrToIntl(StringPtr pstr, ODIText** intlText)
  107. {
  108.     ODIText *result;
  109.     if( intlText && *intlText ) {
  110.         result = *intlText;
  111.         SetITextString(result,pstr);
  112.         SetITextScriptCode(result, smRoman);
  113.         SetITextLangCode(result, langEnglish);
  114.     } else {
  115.         result = CreateIText(smRoman,langEnglish,pstr);
  116.         if( intlText )
  117.             *intlText = result;
  118.     }
  119.     return result;
  120. }
  121.  
  122. //------------------------------------------------------------------------------
  123. // PStrToText
  124. //------------------------------------------------------------------------------
  125.  
  126. ODHandle PStrToText(ConstStr255Param pstr)
  127. {
  128.     ODHandle textHandle = kODNULL;
  129.     
  130.     if ( pstr )
  131.     {
  132.         textHandle = ODNewHandle(pstr[0]);
  133.         if ( textHandle )
  134.         {
  135.             ODPtr textPtr = ODLockHandle(textHandle);
  136.             ODBlockMove((ODPtr)&(pstr[1]), textPtr, pstr[0]);
  137.             ODUnlockHandle(textHandle);
  138.         }
  139.     }
  140.  
  141.     return textHandle;
  142. }
  143.  
  144. //------------------------------------------------------------------------------
  145. // TextToPStr
  146. //------------------------------------------------------------------------------
  147.  
  148. void TextToPStr(ODHandle textHandle, Str255 destString)
  149. {
  150.     if ( textHandle )
  151.     {
  152.         ODUShort textSize = GetHandleSize((Handle) textHandle);
  153.         if (textSize > 255)
  154.             textSize = 255;
  155.         
  156.         ODBlockMove(*textHandle, (ODPtr) &destString[1], textSize);
  157.         destString[0] = textSize;
  158.     }
  159.     else
  160.     {
  161.         destString[0] = 0;
  162.     }
  163. }
  164.  
  165. //------------------------------------------------------------------------------
  166. // ClipStringToBytes
  167. //------------------------------------------------------------------------------
  168.  
  169. ODSShort ClipStringToBytes( Str255 string, ODSShort numBytes,
  170.         ODScriptCode scriptCode )
  171. {
  172.     // point at the proposed last character of the string.  Ask what it is.
  173.     // If the answer is SecondByte, we're done.  If it's SingleByte, we're
  174.     // also done.  If it's FirstByte, we back up one and then we're done.
  175.     // Note that this use of CharacterByteType may cause it to erroneously
  176.     // identify a secondByte as a singleByte, but that that's ok in this case
  177.     // as both are treated the same.
  178.  
  179.     Ptr lastByte = (Ptr)&string[numBytes];
  180.     short cbResult = CharacterByteType( lastByte, 0, scriptCode );
  181.     if ( cbResult == smFirstByte )
  182.         --numBytes;
  183.  
  184.     string[0] = numBytes;
  185.     return numBytes;
  186. }
  187.  
  188.